package com.hotel.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotel.annotation.ControllerEndpoint;
import com.hotel.entity.Category;
import com.hotel.response.Result;
import com.hotel.service.CategoryService;
import com.hotel.vo.CategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author Lin
 * @since 2022-05-30
 */

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation(tags = "菜单",value = "获取所有菜单")
    @GetMapping("/getCategoryList")
    public Result getCategoryList(){
        List<Category> categoryList = categoryService.getCategoryList();
        return Result.ok().data("categoryList",categoryList);
    }

    @ApiOperation(tags = "菜单",value = "增加菜单")
    @RequiresPermissions({"konwledge:treeAdd"})
    @ControllerEndpoint(exceptionMessage = "增加菜单失败", operation = "增加菜单")
    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody CategoryVO categoryVO){
        int i = categoryService.addCategory(categoryVO);
        return Result.ok().data("addCategory",i);
    }

    @ApiOperation(tags = "菜单",value = "菜单图上传")
    @PostMapping("/uploadCategory")
    public Result uploadCategory(@RequestParam("file") MultipartFile file){
        String s = categoryService.upload(file,"category_image");
        return Result.ok().data("url",s);
    }

    @ApiOperation(tags = "菜单",value = "编辑菜单信息")
    @GetMapping("/editCategory")
    public Result editCategory(@RequestParam("id") String id){
        Category category = categoryService.editCategory(id);
        return Result.ok().data("editCategory",category);
    }

    @ApiOperation(tags = "菜单",value = "修改菜单信息")
    @RequiresPermissions({"konwledge:treeUpdate"})
    @ControllerEndpoint(exceptionMessage = "修改菜单失败", operation = "修改菜单")
    @PutMapping("/updateCategory")
    public Result updateCategory(@RequestBody CategoryVO categoryVO){
        int i = categoryService.updateCategory(categoryVO);
        return Result.ok().data("updateCategory",i);
    }

    @ApiOperation(tags = "菜单",value = "删除菜单信息")
    @RequiresPermissions({"konwledge:treeDelete"})
    @ControllerEndpoint(exceptionMessage = "删除菜单失败", operation = "删除菜单")
    @DeleteMapping("/deleteCategory")
    public Result deleteCategory(@RequestParam("id") String id){
        int i = categoryService.deleteCategory(id);
        return Result.ok().data("deleteCategory",i);
    }


    @ApiOperation(tags = "导航栏",value = "获取导航列表")
    @GetMapping("/getNavList")
    public Result getNavList(@RequestParam(value = "name",required = false) String name,
                             @RequestParam(value = "status",required = false)Integer status,
                             @RequestParam(value = "current",required = false,defaultValue = "1")Integer current,
                             @RequestParam(value = "size",required = false,defaultValue = "4")Integer size) {
        IPage<Category> navList = categoryService.getNavList(name, status, current, size);
        return Result.ok().data("navList",navList);
    }

    @PostMapping("/addNav")
    @RequiresPermissions({"navigation:add"})
    @ApiOperation(tags = "导航栏",value = "增加导航信息")
    @ControllerEndpoint(exceptionMessage = "增加导航失败", operation = "增加导航")
    public Result addNav(@RequestBody CategoryVO categoryVO){
        System.err.println(categoryVO);
        int i = categoryService.addNav(categoryVO);
        return Result.ok().data("addNav",i);
    }

    @PostMapping("/updateStatus")
    @RequiresPermissions({"navigation:status"})
    @ControllerEndpoint(exceptionMessage = "更新导航状态失败", operation = "更新导航状态")
    @ApiOperation(tags = "导航栏",value = "修改导航状态")
    public Result updateStatus(String id,Integer status){
        int i = categoryService.updateStatus(id, status);
        return Result.ok().data("updateStatus",i);
    }

    @GetMapping("/editNav")
    @ApiOperation(tags = "导航栏",value = "编辑导航信息")
    public Result editNav(String id){
        Category category = categoryService.editNav(id);
        return Result.ok().data("editNav",category);
    }

    @PutMapping("/updateIcon")
    @RequiresPermissions({"navigation:update"})
    @ApiOperation(tags = "导航栏",value = "修改导航信息")
    @ControllerEndpoint(exceptionMessage = "更新导航失败", operation = "更新导航")
    public Result updateNav(@RequestBody CategoryVO categoryVO){
        int i = categoryService.updateNav(categoryVO);
        return Result.ok().data("updateStatus",i);
    }


    @DeleteMapping("/deleteNavById")
    @RequiresPermissions({"navigation:delete"})
    @ControllerEndpoint(exceptionMessage = "删除导航失败", operation = "删除导航")
    @ApiOperation(tags = "导航栏",value = "根据id删除导航栏信息")
    public Result deleteNavById(String id){
        int i = categoryService.deleteNavById(id);
        return Result.ok().data("deleteNav",i);
    }



}

